//
//  HisViewController.m
//  test
//
//  Created by 123456 on 2017/8/28.
//  Copyright © 2017年 lyh. All rights reserved.
//

#import "ReportListViewControllerDemo.h"
#import "EcgReportViewController.h"
#import "EcgHeader.h"
#import <IKLibrary/IKLibrary.h>


@interface ReportListViewControllerDemo ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *array;
@end

@implementation ReportListViewControllerDemo
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"心电检测历史";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupTableView];
    self.array = [[NSMutableArray alloc] init];
    [self loadHistory];
}

- (void)loadHistory{
    [self.array removeAllObjects];
    NSString *filePath = [IKPublicUtil currentECGDataFileDir];
    NSLog(@"filePath---%@",filePath);
    NSFileManager *manager = [NSFileManager defaultManager];
    NSDirectoryEnumerator *direnum = [manager enumeratorAtPath:filePath];
    NSString *filename ;
    if(self.type == ReportListTypeZip){
        while (filename = [direnum nextObject]) {
            if([[filename pathExtension] isEqualToString:@"tar"] || [[filename pathExtension] isEqualToString:@"zip"]){
                [self.array addObject:[NSString stringWithFormat:@"%@/%@",filePath,filename]];
            }
        }
    }else{
        while (filename = [direnum nextObject]) {
            if([[filename pathExtension] isEqualToString:@"tar"]){
                [self.array addObject:[NSString stringWithFormat:@"%@/%@",filePath,filename]];
            }
        }
    }
   
    [self.array sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSString *file1 = ((NSString*)obj1).lastPathComponent.stringByDeletingPathExtension;
        NSString *file2 = ((NSString*)obj2).lastPathComponent.stringByDeletingPathExtension;
        if ([file1 integerValue] < [file2 integerValue]){
            return NSOrderedDescending;
        }else{
            return NSOrderedAscending;
        }
    }];
    [self.tableView reloadData];
}

- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.backgroundColor = [UIColor clearColor];
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
/**
 分析结果没有保存在文件中，需要自己保存
 */
- (NSString*)getTestSmartResult{
    NSString *str = @"{\"ecgval\":{\"hrmean\":\"65\",\"breath\":\"19\",\"pbcnt\":\"0\",\"hrmax\":\"69\",\"suspectedflag\":true,\"heartage\":\"34\",\"pressure\":\"75\",\"hrv\":\"16\",\"hrmin\":\"61\"},\"ecgsymps\":[{\"sympcode\":\"0.0\",\"name\":\"未见异常\",\"suggestion\":\"本次测试心电图波形规律,未见异常。建议您：保持养成良好生活习惯，多吃绿色蔬菜、水果，定期运动、定时排便，避免熬夜。\"},{\"sympcode\":\"0.003\",\"name\":\"焦虑\",\"suggestion\":\"您的心理状态偏向焦虑，可能：保持养成良好生活习惯，多吃绿色蔬菜、水果，定期运动、定时排便，避免熬夜。\"},{\"sympcode\":\"0.003\",\"name\":\"焦虑\",\"suggestion\":\"您的心理状态偏向焦虑，可能引起的原因：工作生活压力、频繁应酬、加班熬夜。长期以往会对生理和心理上都会产生不同程度的损害，会影响免疫系统，易于患病。建议改善习惯、保持心情放松、适度运动、多做深呼吸。\"}]}";
    return str;
}

#pragma mark --UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.array.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = self.array[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ecgFilePath = self.array[indexPath.row];
    NSString *key = ecgFilePath.lastPathComponent;
    
    //分析结果获取,用户应该自定实现,Demo 中存在本地
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *smartResult = [userDefaults objectForKey:key];
    if([IKPublicUtil isEmpty:smartResult]){
        smartResult =  [self getTestSmartResult];
    }
    NSLog(@"ecgFilePath---%@",ecgFilePath);
    NSFileManager *manager = [NSFileManager defaultManager];
    if(self.type == ReportListTypeResult){
        IKEcgReportController *controller = [IKEcgReportController getInstance];
        [controller showEcgReport:self ecgSmartResult:smartResult tarFilePath:ecgFilePath];
    }
    else if(self.type == ReportListTypeImage){
        IKEcgReportController *controller = [IKEcgReportController getInstance];
        [controller showEcgImage:self ecgSmartResult:smartResult tarFilePath:ecgFilePath];
    }
   
}
@end
